<?php
namespace Admin\Controller;
class PositionController extends CommonController {
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('Position');
    }

    public function _before_index(){
        $cate_id = I('get.cate_id',0,'int');
        $big_menu = array(
            'title' => '添加信息',
            'iframe' => U('Position/add',array('cate_id'=>$cate_id)),
            'id' => 'add',
            'width' => '500',
            'height' => '360'
        );
        $this->_exclude = 'edit';

        $this->assign('big_menu',$big_menu);
    }

    protected function _search() {
        $map = array();
        $cate_id = I('get.cate_id',0,'int');
        $type_id = I('get.type_id',0,'int');
        if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        if ($cate_id) {
            $map['cate_id'] = array('eq',$cate_id);
        }
        if($type_id){
            $map['type_id'] = array('eq',$type_id);
        }
        $this->assign('search', array(
            'cate_id' => $cate_id,
            'status'  => $status,
            'type_id' => $type_id
        ));
        return $map;
    }

    public function _before_add(){
        $cate_id = I('get.cate_id',0,'int');
        $model = $this->publicGetModel($cate_id);
        $this->assign('model',$model);
        $this->assign('cate_id',$cate_id);
    }

    public function _before_insert($data){
        $this->addAll = true;
        $house_id = I('post.house_id');
        $house_id = trim($house_id,',');
        $arr = (array)explode(',',$house_id);
        $insert = array();
        if(!isset($data['type_id'])){
            $data['type_id'] = 0;
        }
        $model = I('post.model','');
        if(!empty($arr)){
            D($model)->where(array('id'=>array('in',$arr)))->save(array('is_index'=>1));
            foreach($arr as $v){
                if($model=='article'){
                    $info = getArticleName($v,true,'id,title,img');
                }else{
                    $info =  getHouseName($v,true,'*',$model);
                }
                if($model=='house'){
                    $add = explode('|',$info['address']);
                    $info['add1'] = $add[0];
                }
                if($model=='used' or $model=='rental'){
                    $info['estate'] = getEstate($info['estate_id'],'title');
                }
                $title = (isset($data['title']) && !empty($data['title'])) ? $data['title'] : $info['title'];
               $insert[] = array('hid'=>$v,'model'=>$model,'cate_id'=>$data['cate_id'],'data'=>serialize($info),'add_time'=>time(),'title'=>$title);
            }
        }

        return $insert;
    }

    public function _before_edit(){
        $this->_before_add();
    }

    private function publicGetModel($id){
        $info = M('PositionCate')->where(array('id'=>$id))->getField('model');
        return $info;
    }

    public function delete(){
        $mod = D($this->_name);
        $pk = $mod->getPk();
        $ids = trim(I($pk), ',');
        if ($ids) {
           $info = $this->_mod -> where(array('id'=>array('in',$ids)))->field('hid,model')->select();
            if (false !== $mod->delete($ids)) {
                if($info){
                    $modelarr = array();
                    foreach($info as $v){
                        $modelarr[$v['model']][] = $v['hid'];
                    }
                    foreach($modelarr as $k=>$v){
                        D($k)->where(array('id'=>array('in',$v)))->save(array('is_index'=>0));
                    }
                }
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
            $this->error(L('illegal_parameters'));
        }
    }
}